home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Nested Structures in C - A Question
- Date: 28 Jan 1996 11:44:08 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4egjm8INNnpp@keats.ugrad.cs.ubc.ca>
- References: <36400002@peg>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <36400002@peg>, <tmccoy@peg.apc.org> wrote:
- >A Question on Nested Structures in C
- >------------------------------------
- >
- >I've recently been doing some work on the design of the C
- >programming language and there seems to be an inconsistency
- >in the way nested structures are defined.
-
- Your newsreader is adding ^M characters to every line.
-
- >If I want to declare a simple structure called "outer" I
- >can do the following:
- >
- >struct outer {
- > int var1;
- > int var2;
- > int var3;
- >};
- >
- >Apparently the compiler will NOT allocate any storage when
- >I do this because, according to Kernighan and Ritchie (2nd
- >Ed), "a structure declaration that is not followed by a
- >list of variables reserves no storage; it merely describes
- >a template or the shape of a structure" (Page 128).
-
- Yes, that is right.
-
- >This is all well and good. And when I want to define a new
- >structure *within* my old one, I should be able to do:
- >
- >struct outer {
- > int var1;
- > int var2;
- > int var3;
- > struct inner {
- > int nested1;
- > int nested2;
- > };
- >};
- >
- >and I should be able to define an instance of my structure
- >by doing:
- >
- >struct outer instance;
- >
- >and refer to the first member of my inner structure by doing:
- >
- >instance.inner.nested1
-
- No, because inner is just a structure label, not an member name.
-
- >Yet, C won't let me do this!! It insists that I define my
- >nested structure as follows:
- >
- >struct outer {
- > int var1;
- > int var2;
- > int var3;
- > struct inner {
- > int nested1;
- > int nested2;
- > } DUMMY;
- >};
-
- In fact, you can dispense with the word "inner", since the structure "outer"
- will act as a template for _all_ the contents.
-
- >and after doing
- >
- >struct outer instance;
- >
- >I must refer to the first member of my inner structure by
- >doing:
- >
- >instance.DUMMY.nested1
- >
- >i.e. I must access the inner members via a variable (called
- >"DUMMY") instead of being able to use my structure tag
- >(i.e. "inner"). In fact, the *only way* I can access the
- >inner members is by defining the DUMMY variable within the
- >structure template of "outer".
-
- Right, you can never use structure tags as variable names (unless you declare a
- structure variable with the same name as its tag). Unless a variable or type
- called "foo" is declared, the only way the structure label "foo" has a meaning
- is when it appears to the right of "struct" in a valid syntactic construct.
-
- >I am just wondering whether anybody else has found this to
- >be strange and if anybody knows why the nested structure
- >feature has been designed in this way.
-
- >I'm also curious about how compilers would implement this
- >feature.
-
- Perhaps you would be more interested in structure inheritance. You declare a
- base structure, and then declare another structure as an extension of that one.
- Have a look at Oberon. Oberon lets you derive RECORDs this way, by using the
- fields of an existing record as the initial fields of a new record without
- nesting.
-
- The declaration goes something like:
-
- TYPE foo = RECORD
- X : INTEGER;
- Y : INTEGER;
- END;
-
- TYPE bar = RECORD (foo)
- Z : INTEGER;
- W : INTEGER;
- END;
-
- The record type "bar" has elements X, Y, Z and W all on one "level".
- If you find nesting obnoxious, you might like such a feature. The "foo" in
- brackets means to use foo as a base to which the new fields are added.
-
- >With a nested structure, as with a simple structure, I
- >*should* be able to define a pure structure template in
- >which no storage is allocated.
-
- Incorrect. A C structure is a collection of variables. A variable is something
- that has storage. In essence, a structure is a stamp for allocating storage.
- A structure cannot contain a blank template of another structure, because such
- a template is not a variable.
-
- It's like in mathematics. Say I have a set of THINGS which I recursively define
- to be numbers, or sets of more THINGS. Furthermore I can give it a type name
- which helps me mark identical sets of THINGS by the same name. Now, does it
- make sense for such a name to also be a member of a set of THINGS? Clearly not,
- since it is neither a number, nor a set of THINGS.
-
- > Yet, in the definition I am
- >forced to use above, strictly speaking the compiler should
- >allocate storage for "DUMMY" even though the enclosing
- >structure (i.e. "outer") is only a template.
-
- You don't call it "dummy", typically. You normally it a meaningful name.
-
- >I find this very inconsistent and it is a feature that bugs
- >me about an otherwise elegant language.
-
- Nothing inconsistent about it.
-
- >Does it bother anybody else? Or am I just looking at it
- >all the wrong way?
-
- It's a little bothersome to have to type the extra name to dereference the
- inside structure, for lack of inheritance.
- --
-
-